<?php

use com\gdl\newagk\interfaces\AuthorityServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\newagk\dataobject\authority\AuthorityDTO;
use com\gdl\newagk\dataobject\authority\AuthorityRequestDTO;


class api_AuthorityService extends api_Abstract implements AuthorityServiceIf
{
	
	/*获取权限
	*@param int userid 登陆的管理员id
	*@param string reg 管理员标识
	*@param int roleid 权限组
	*/
	public function verRbac(AuthorityDTO $authority)
    {
		$result = new ResultDO();
		$adminUserId =$authority->userid ? $authority->userid : 0;
		$adminReg = $authority->adminReg ? $authority->adminReg : '';
		$roleid =$authority->roleid ? $authority->roleid : 0;
		$token =$authority->code ? $authority->code : '';
		$result->success = true;
		$result->code = 0;
		if(!$adminUserId || $token==''){
			$result->message = '缺少参数';
			return $result;
		}

		try {
			/*****************/
			$nodes = new model_lhbase_adminnode();
			$adminuser = new model_lhbase_appadminuser();
			$authadmintoken = new model_lhbase_authadmintoken();
			$tokenCode = $authadmintoken->selectOne('adminUserId='.$adminUserId.' and endTime>'.time(),'id,token');

			  if (!empty($tokenCode['id']) && $tokenCode['token']==$token) {
				$result->message = '通过';
				$result->code = 1;
				 } else {
					$result->message = '过期或不存在';
			}
			return $result;

			$admin = $adminuser->selectOne(['id'=>$userid,'adminReg'=>$adminReg],'id,characters');
			if(empty($admin['id'])){
				$result->message = '账号错误';
				return $result;
			}
			$node = $nodes->getMenu($admin['characters'],$admin['id'],$roleid);
			/******end*********/

            if (!empty($node)) {
				$result->data = $node;
				$result->message = $admin['characters'];
				$result->code = 1;
             } else {
				$result->message = '没有权限';
		}
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = 0;
            $result->message = '未知错误';
        }
        return $result;
	}

	/*获取权限列表【这个是新版的】
	*@param int filename['adminUserId'] 登陆的管理员id
	*@param string filename['adminReg'] 管理员标识
	*@param int filename['moduleId'] 组id
	*@param int filename['unitId'] 部门id
	*@param int filename['appId'] 标识id 
	*@param int filename['pid'] 上级id
	*/
	public function accessList(AuthorityRequestDTO $authority)
    {

		$result = new ResultDO();
		$adminUserId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$groupId = $authority->filename['groupId'] ? gdl_lib_BaseUtils::getStr($authority->filename['groupId'],'int') : 0;
		$appId = $authority->filename['appId'] ? gdl_lib_BaseUtils::getStr($authority->filename['appId'],'int') : 0;
		$pid = $authority->filename['pid'] ? gdl_lib_BaseUtils::getStr($authority->filename['pid'],'int') : 0;
		
		$result->code = 0;
		$result->success = true;

		if(!$adminUserId || $adminReg=='' || !$appId){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			$agkunits = new model_lhbase_units();
			$adminuser = new model_lhbase_appadminuser();
			$adminrole = new model_lhbase_authgroup();
			$appmenu = new model_lhbase_appmenu();
			$authrule = new model_lhbase_authrule();
			$authgroupaccess = new model_lhbase_authgrouprule();
			$group = $adminrole->select(['id'=>$groupId],'id,title,rules,unitsId')->items;
			$types = $agkunits->enabled($adminUserId,$adminReg);
//,$group[0]['unitsId'] //暂时去掉这个
			if($types['code']!='y'){
				$result->message = $types['msg'];
				return $result;
			}
			
			 $authRoleId = $authgroupaccess->getInfoArg(['groupId'=>$groupId],'ruleId');
			$roleIdArrs =  $roleIdArr = empty($authRoleId) ? [0] : array_column($authRoleId,'ruleId');
			$navLists = $authrule->select(' 1 ', 'id,pid,title')->items;
			if ($types['mark']['characters']=='gl' || ($types['mark']['characters']=='zg' && $types['mark']['unitsId'])) {
				//获取用户所拥有的权限
				$navList = $authrule->getMenu($adminUserId,$appId);
				$authRuleId = array_column($navList,'id');
				$authRulePid = array_column($navList,'pid');
				$authRuleId = array_merge($authRuleId,$roleIdArr);
				$parentId = [0];
				foreach($navLists as $v){
					$parentId[$v['id']] = $v['pid'];
				}
				$authRulePid = self::lower_level_button($parentId,$authRulePid);
				$roleIdArrs =  array_merge($roleIdArrs,array_values($authRulePid));
				$authRulePid = array_merge($authRuleId,array_values($authRulePid));
				/****获取因为层级原因也不能给用户显示的节点****/
				$endRule = $authrule->select('endLevel<'.$types['mark']['level'],'id')->items;
				$endRuleId =[0];
		
				if(!empty($endRule)){
					
					$endRuleId = $authrule->dnRule(array_column($endRule,'id'));
				
				}
				
				$authRulePid = array_diff($authRulePid,$endRuleId);
				/****获取因为层级原因也不能给用户显示的节点 end****/
			} else {
				$navList = &$navLists;
				$authRuleId = array_column($navList,'id');
				$authRulePid = &$authRuleId;

			}

			$res = []; 
		//	$res = $authrule->select('id in('.implode(',',$authRulePid).')', 'id,pid,id ruleId,title','group by sort asc')->items;
				

			//生成数据格式
			$node_tree = [];
			if($types['mark']['characters']=='zg' && !$types['mark']['unitsId']){

				foreach ($navLists as $v) {
					$checkstateid = 0;
					if(in_array($v['id'], $roleIdArr)){
						$checkstateid = 1;
					}
					
					$node_tree[] = [
						"id"          => $v['id'],
						"pid"         => $v['pid'],
						"title"        => $v['title'],
						"value"       => $v['id'],
						'operating'=>1,
						'display'=>1,
						'checked'  =>  $checkstateid,
					];
				}
				
			} else {
				$useraccess_nodeid = array_column($navList,'id');
				foreach ($navLists as $v) {
				$checkstateid = 0;
				if(in_array($v['id'], $roleIdArrs)){
						$checkstateid = 1;
				}
				$node_tree[] = [
						"id"          => $v['id'],
						"pid"         => $v['pid'],
						"title"        => $v['title'],
						"value"       => $v['id'],
						'display'=>in_array($v['id'],$authRulePid) ? 1 : 0,
						'operating'=>in_array($v['id'],$useraccess_nodeid) ? 1 : 2,
						'checked'  =>  $checkstateid,
					];
				
				}
				
			}
			

            if ($node_tree) {
				$result->data = $node_tree;
				$result->datas[] = $roleIdArr;
				$result->message = '成功';
				$result->code = 1;
            } else {
				$result->code = 1;
				$result->message = '没有数据';
             
            }
      
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}

	/*获取权限组列表
	*@param int filename['adminUserId'] 登陆的管理员id
	*@param string filename['adminReg'] 管理员标识
	*@param int filename['unitsid'] 部门id
	*/
	
	public function autList(AuthorityRequestDTO $authority)
    {

        $result = new ResultDO();
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg']) : '';
		$title = $authority->filename['title'] ? gdl_lib_BaseUtils::getStr($authority->filename['title']) : '';
		$userid = $authority->filename['adminuserid'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminuserid'],'int') : 0;
		$appId = $authority->filename['appId'] ? gdl_lib_BaseUtils::getStr($authority->filename['appId'],'int') : 0;
		$result->code = 0;
		$result->success = true;
		if(!$userid || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			$agkunits = new model_lhbase_units();
			$adminrole = new model_lhbase_authgroup();
			$authuserunits = new model_lhbase_authuserunits();
			$types = $agkunits->enabled($userid,$adminReg,0);
			if($types['code']!='y'){

				$result->message = $types['msg'];
				return $result;
			}
			
			$condition = 'platformId='.$types['mark']['platformId'].' and appId='.$appId;
			if($types['mark']['characters']!='zg'){
				$condition .= ' and attribution='.$userid;
			}
			
			if($title!=''){
				$condition .= " and title like '%{$title}%'";
			}
		
			$list = $adminrole->select($condition,'id,title,status')->items;

		$result->code = 1;
		if ($list) {
			$result->data = $list;
			$result->message = '成功';
		} else {
			$result->message = '没有数据';
             
		}
      
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}

	/*获取资源列表
	*@param int filename['adminUserId'] 登陆的管理员id
	*@param string filename['adminReg'] 管理员标识
	*@param int filename['groupid'] 组id
	*@param string filename['typesof'] 资源类型
	*@param string filename['title'] 标题名
	*/
	public function resourcList(AuthorityRequestDTO $authority)
    {

        $result = new ResultDO();

		$userid = $authority->filename['adminuserid'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminuserid'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$roleId = $authority->filename['groupid'] ? gdl_lib_BaseUtils::getStr($authority->filename['groupid'],'int') : 0;

		$typesof = $authority->filename['typesof'] ? gdl_lib_BaseUtils::getStr($authority->filename['typesof'],'string') : '';
		
		$title = $authority->filename['title'] ? gdl_lib_BaseUtils::getStr($authority->filename['title'],'string') : '';
		$result->code = 0;
        $result->success = true;
		if(!$userid || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			if(!in_array($typesof,array('qbank','paper','basic','attendance'))){
				$result->code = 1;
				$result->message='没有数据';
				return $result;
			}
			$adminuser = new model_lhbase_appadminuser();
			$units = new model_lhbase_units();
			$adminaccessresourc = new model_newagk_adminaccessresourc();
			$admin = $adminuser->selectOne(['id'=>$userid,'adminReg'=>$adminReg],'id,characters,platformId,unitId');
			
			if($admin['characters']=='zg' || $typesof=='attendance'){
				$unitsId = $units->adminunits($admin['platformId']);

			} else {
				$unitsId = $units->operating($admin['platformId'],$admin['unitId']);
			}
			
			$unitsName = $units->unitsDetails($admin['platformId'],$unitsId);

			$resourc_id = $adminaccessresourc->getMenu($roleId,$admin['platformId'],$unitsId,$typesof);
			
			$resourc = $adminaccessresourc->resourceSystem($admin['characters'],$admin['platformId'],$resourc_id,$admin['id'],$typesof,$title);

            if (!empty($resourc)) {
				$result->data = $resourc;
				$result->datas[] = $unitsName;
				$result->message = '成功';
				$result->code = 1;
            } else {
				$result->message = '失败';
             
            }
      
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	//获取用户权限列表【新版】
	public function accessuserList(AuthorityRequestDTO $authority)
    {

		$result = new ResultDO();
		$adminUserId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$appId = $authority->filename['appId'] ? gdl_lib_BaseUtils::getStr($authority->filename['appId'],'int') : 0;
		$userId = $authority->filename['userId'] ? gdl_lib_BaseUtils::getStr($authority->filename['userId'],'int') : 0;
		
		$result->code = 0;
		$result->success = true;

		if(!$adminUserId || $adminReg=='' || !$userId || !$appId){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			$agkunits = new model_lhbase_units();
			$adminuser = new model_lhbase_appadminuser();
			$adminrole = new model_lhbase_authgroup();
			$appmenu = new model_lhbase_appmenu();
			$authrule = new model_lhbase_authrule();
			$authgrouprule = new model_lhbase_authgrouprule();
			$authgroupaccess = new model_lhbase_authgroupaccess();
			$authuseraccess = new model_lhbase_authuseraccess();
			$types = $agkunits->enabled($adminUserId,$adminReg,0);
			if($types['code']!='y'){
				$result->message = $types['msg'];
				return $result;
			}

			 $authRoleId = $authuseraccess->getInfoArg(['userId'=>$userId,'appId'=>$appId],'ruleId');
			 $roleIdArr = empty($authRoleId) ? [0] : array_column($authRoleId,'ruleId');
			 
			  $groupRoleId = $authgroupaccess->getInfoArg(['groupId'=>$groupId],'ruleId');
			 $groupArr = empty($groupRoleId) ? [0] : array_column($groupRoleId,'ruleId');
			 $roleIdArr = array_merge($roleIdArr,$groupArr);
			$navLists = $authrule->select(' 1 ', 'id,pid,title')->items;
			if ($types['mark']['characters']!='zg') {
				//获取用户所拥有的权限
				$navList = $authrule->getMenu($adminUserId,$appId);
				$authRuleId = array_column($navList,'id');
				$authRulePid = array_column($navList,'pid');
				$authRuleId = array_merge($authRuleId,$roleIdArr);
				$parentId = [0];
				foreach($navLists as $v){
					$parentId[$v['id']] = $v['pid'];
				}
				
				
				$authRulePid = self::lower_level_button($parentId,$authRulePid);
				$authRulePid = array_merge($authRuleId,array_values($authRulePid));
				
			} else {
				$navList = &$navLists;
				$authRuleId = array_column($navList,'id');
				$authRulePid = &$authRuleId;

			}
			//生成数据格式
			$node_tree = [];
			if($types['mark']['characters']=='zg'){

				foreach ($navLists as $v) {
					$checkstateid = 0;
					if(in_array($v['id'], $roleIdArr)){
						$checkstateid = 1;
					}
					
					$node_tree[] = [
						"id"          => $v['id'],
						"pid"         => $v['pid'],
						"title"        => $v['title'],
						"value"       => $v['id'],
						'operating'=>1,
						'display'=>1,
						'checked'  =>  $checkstateid,
					];
				}
				
			} else {
				$useraccess_nodeid = array_column($navList,'id');
				foreach ($navLists as $v) {
				$checkstateid = 0;
				if(in_array($v['id'], $roleIdArr)){
						$checkstateid = 1;
				}
				$node_tree[] = [
						"id"          => $v['id'],
						"pid"         => $v['pid'],
						"title"        => $v['title'],
						"value"       => $v['id'],
						'display'=>in_array($v['id'],$authRulePid) ? 1 : 0,
						'operating'=>in_array($v['id'],$useraccess_nodeid) ? 1 : 2,
						'checked'  =>  $checkstateid,
					];
				
				}
				
			}
			

            if ($node_tree) {
				$result->data = $node_tree;
				$result->datas[] = $roleIdArr;
				$result->message = '成功';
				$result->code = 1;
            } else {
				$result->code = 1;
				$result->message = '没有数据';
             
            }
      
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	
	/*public function accessList(AuthorityRequestDTO $authority)
    {
        $result = new ResultDO();
		//$uid = $authority->id;
		$user_name = $authority->username;
		$platform_id = $authority->filename['platform_id'];
		$userid = $authority->filename['userid'];
		$role_id = $authority->id;
		try {*/
			/*****************/
				  //分组信息
			/*$admingroup = new model_lhbase_adminmodule();
			$list_group = $admingroup->getList();
			$group = self::reset_by_key_admin($list_group, "id");
			//节点信息
			$where_node['status'] = 1;
			$where_node['isdelete'] = 0;
			//对于非超级管理员用户只显示其拥有所有权限的节点
			$AdminAccess = new model_lhbase_adminaccess();
			$adminroleuser = new model_lhbase_adminroleuser();
			if ($userid>1 && $userid) {
				$leftJoin = array(
                $adminroleuser->tableName().' as role_user' => 'role_user.role_id = '.$AdminAccess->tableName().'.role_id'
				);
				$access_node = $AdminAccess->select('role_user.user_id='.$userid, 'access.node_id','', '', $leftJoin)->items;
				$where_node['id'] = ["in", self::filter_value_admin($access_node, "node_id")];
			}

			$condition = ["status"=>1,'isdelete'=>0,'admin_reg'=>'1'];
			//$condition = " status=1 and isdelete=0 and pid>0";
			$adminroleuser = new model_lhbase_adminroleuser();
			if($user_name!='admin'){
				//获取管理员的权限组
				//$roleid = $adminroleuser->selectOne(['user_id'=>$userid],'role_id');
				$nodeIds = $AdminAccess->select(['role_id'=>$role_id], 'node_id')->items;
				if (!empty($nodeIds)){
					$nodeIds = array_column($nodeIds,'node_id');
					$condition['id'] = ['in',$nodeIds];
					//$condition .= ' and id in('.implode(',',$nodeIds).')';
				}
			}

			$adminnode = new model_lhbase_adminnode();
			$node = $adminnode->select($condition, 'id,pid,group_id,name,title,level,type')->items;

			$accesses = $AdminAccess->select(['role_id'=>$role_id], '*')->items;
			$accesses_node = self::filter_value_admin($accesses, "node_id");


			//生成数据格式
			$node_tree = [];
			foreach ($node as $v) {
				$checkstateid = 0;
				if(in_array($v['id'], $accesses_node)){
					$checkstateid = 1;
				}*/
				/*
				$node_tree[] = [
					"id"          => $v['id'],
					"pid"         => $v['pid'],
					"text"        => $v['title'] . " (" . $v['name'] . ") " . (isset($group[$v['group_id']]) ? '<span style="color:red">[ ' . $group[$v['group_id']]['name'] . ' ]</span>' : ''),
					"value"       => $v['id'] . "_" . $v['level'] . "_" . $v['pid'],
					"showcheck"   => true,
					'checkstate'  =>  $checkstateid,
					'hasChildren' => $v['type'] ? true : false,
					'isexpand'    => true,
					'complete'    => true,
				];
				*/

/*
				$node_tree[] = [
					"id"          => $v['id'],
					"pid"         => $v['pid'],
					"text"        => $v['title'],
					"value"       => $v['id'] . "_" . $v['level'] . "_" . $v['pid'],
					"showcheck"   => true,
					'checkstate'  =>  $checkstateid,
					'hasChildren' => $v['type'] ? true : false,
					'isexpand'    => true,
					'complete'    => true,
				];
			}

	
			$result->data = $node_tree;*/
			/******end*********/
           /* if ($result) {
                $result->code = 1;
            } else {
                $result->code = 0;
            }
            $result->success = true;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}*/
	
	/*
	//获取用户权限列表
	public function accessuserList(AuthorityRequestDTO $authority)
    {
        $result = new ResultDO();
		//$uid = $authority->id;
		$user_name = $authority->username;

		$userid = $authority->filename['userid'];
		$role_id = $authority->id;
		try {
				  //分组信息
			$admingroup = new model_lhbase_adminmodule();
			$list_group = $admingroup->getList();
			$group = self::reset_by_key_admin($list_group, "id");
			//节点信息
			$where_node['status'] = 1;
			$where_node['isdelete'] = 0;
			//对于非超级管理员用户只显示其拥有所有权限的节点
			$AdminAccess = new model_lhbase_adminaccess();
			$adminroleuser = new model_lhbase_adminroleuser();


			$condition = ["status"=>1,'isdelete'=>0,'admin_reg'=>'1'];

			if($user_name!='admin'){
				//获取管理员的权限组
				//$roleid = $adminroleuser->selectOne(['user_id'=>$userid],'role_id');
				$nodeIds = $AdminAccess->select(['role_id'=>$role_id], 'node_id')->items;
				if (!empty($nodeIds)){
					$nodeIds = array_column($nodeIds,'node_id');
					$condition['id'] = ['in',$nodeIds];
					//$condition .= ' and id in('.implode(',',$nodeIds).')';
				}
			}

			$adminnode = new model_lhbase_adminnode();
			$node = $adminnode->select($condition, 'id,pid,group_id,name,title,level,type')->items;
			$adminuseraccess = new model_lhbase_appadminuseraccess();
			$accesses = $adminuseraccess->select(['role_id'=>$role_id], '*')->items;
			$accesses_node = self::filter_value_admin($accesses, "node_id");


			//生成数据格式
			$node_tree = [];
			foreach ($node as $v) {
				$checkstateid = 0;
				if(in_array($v['id'], $accesses_node)){
					$checkstateid = 1;
				}
	


				$node_tree[] = [
					"id"          => $v['id'],
					"pid"         => $v['pid'],
					"text"        => $v['title'],
					"value"       => $v['id'] . "_" . $v['level'] . "_" . $v['pid'],
					"showcheck"   => true,
					'checkstate'  =>  $checkstateid,
					'hasChildren' => $v['type'] ? true : false,
					'isexpand'    => true,
					'complete'    => true,
				];
			}

	
			$result->data = $node_tree;


            if ($result) {
                $result->code = 1;
            } else {
                $result->code = 0;
            }
            $result->success = true;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	*/
	
	//组授权
	public function authorized(AuthorityRequestDTO $authority)
    {
		$result = new ResultDO();
		$groupId = $authority->filename['groupId'] ? intval($authority->filename['groupId']) : 0;
		$nodeId = $authority->filename['node'] ? json_decode($authority->filename['node'],true) : [];
		$resourc = $authority->filename['resourc'] ? json_decode($authority->filename['resourc'],true) : [];
		$appId = $authority->filename['appId'] ? intval($authority->filename['appId']) : 0;
		$adminUserId = $authority->filename['adminUserId'] ? intval($authority->filename['adminUserId']) : 0;
		
		 $result->success = true;
         
		if(!$groupId || !$adminUserId || !$appId){
			$result->message = '缺少参数';
			$result->code = 0;
			return $result;
		}
		try {
			/************/
			$dbAccess = new model_lhbase_authgrouprule();
            //删除之前的权限分配
			$accessId = [];
			$roleAcess = $dbAccess->select(['groupId'=>$groupId],'ruleId')->items;
			foreach($roleAcess as $vc){
				$accessId[] = $vc['ruleId'];
			}
			$authgroupaccess = new model_lhbase_authgroupaccess();
			$adminuseraccess = new model_lhbase_authuseraccess();
			$appadminuser = new model_lhbase_appadminuser();
			$platformId = $appadminuser->selectOne(['id'=>$adminUserId],'platformId');
			$user = $authgroupaccess->select(['groupId'=>$groupId],'uId userId')->items;
			if(!empty($accessId)){
				$adminuseraccess->delete('groupId='.$groupId.' and ruleId in('.implode(',',$accessId).')');
				
			}

			$dbAccess->delete(array('groupId' => $groupId));/*****新增逻辑查询当前用户下所有有关系到权限的管理员id******/
			$userId = self::subordinateId($groupId);
			/*****新增逻辑查询当前用户下所有有关系到权限的管理员id end******/
			
			/****资源组存在******/
			$authgroupunits = new model_lhbase_authgroupunits();
			$authuserunits = new model_lhbase_authuserunits();
			$authResourc = $authgroupunits->select('groupId='.$groupId,'units')->items;
			$authgroupunits->delete('groupId='.$groupId);
			if(!empty($resourc)){
				if(!empty($authResourc)){
					$nodeIdMiss = array_diff( array_column($authResourc,'units'),$resourc);
					if(!empty($nodeIdMiss)){ //去掉下级所有用户对应的部门
						$authuserunits->delete('adminUserId in('.implode(',',$userId).') and unitsId in('.implode(',',$nodeIdMiss).')');
					}
				
				}
				$insertAll = [];
				foreach($resourc as $v){
						$insertAll[] = [
							"groupId" => $groupId,
							"units" => $v,
							];
						
				}
						if(!empty($insertAll)){
							$authgroupunits->beginTransaction(); //批量写入
							$authuseraInser = $authgroupunits->insertall($insertAll);
							if($authuseraInser){
								$authgroupunits->commit();
							} else {
								$authgroupunits->rollBack();
							}
						}
				
				
				
			}
			/*****资源组存在 end*****/
			
			
            //写入新的权限分配

            if (isset($nodeId) && !empty($nodeId) && is_array($nodeId)) {
			
			if(!empty($accessId)){
				$nodeIdMiss = array_diff($accessId,$nodeId);
				if(!empty($nodeIdMiss)){ //去掉下级所有用户对应的权限
						$adminuseraccess->delete('userId in('.implode(',',$userId).') and ruleId in('.implode(',',$nodeIdMiss).')');
				}
				
			}

			
			
                $insertAll = [];
		 $insertUserAll = [];
                foreach ($nodeId as $v) {
                    $insertAll[] = [
                        "groupId" => $groupId,
                        "ruleId" => $v,
                    ];
			

					if(!empty($user)){
						 foreach ($user as $vu) {
							$insertUserAll[]=[
								'userId'=>$vu['userId'],
								'ruleId'=> $v,
								'groupId'=>$groupId,
								'platformId'=>$platformId['platformId'],
								'appId'=>$appId,
								];
						 }

					}
					
                }
				if(!empty($insertAll)){
					$dbAccess->insertall($insertAll, true);
				}
				 if(!empty($insertUserAll)){
					$authuseraInser = $adminuseraccess->insertall($insertUserAll);
				 }
			
					$result->message = '成功';
					$result->code = 1;
				
            } else {

				$result->message = '成功';
				$result->code = 1;
		}
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	

	/*个人授权
	*@param int userid 登陆的管理员id
	*@param int roleId 权限组id
	*@param int unitId 部门id
	*/
	public function userAuthorized(AuthorityRequestDTO $authority)
    {
		$result = new ResultDO();
		$groupId = $authority->filename['groupId'] ? intval($authority->filename['groupId']) : 0;
		$nodeId = $authority->filename['node'] ? json_decode($authority->filename['node'],true) : [];
		$resourc = $authority->filename['resourc'] ? json_decode($authority->filename['resourc'],true) : [];
		$appId = $authority->filename['appId'] ? intval($authority->filename['appId']) : 0;
		$adminUserId = $authority->filename['adminUserId'] ? intval($authority->filename['adminUserId']) : 0;
		
		 $result->success = true;
         
		if(!$adminUserId || !$appId){
			$result->message = '缺少参数';
			$result->code = 0;
			return $result;
		}
		try {
			/************/
			$adminuseraccess = new model_lhbase_authuseraccess();
			$appadminuser = new model_lhbase_appadminuser();
			//$user = $adminuseraccess->select(['userId'=>$adminUserId],'ruleId')->items; // ?

			//$adminuseraccess->delete('userId='.$adminUserId.' and appId='.$appId);
				
	
			//获取原来的权限
			

			/****资源组存在******/
			$authgroupunits = new model_lhbase_authuserunits();
			$authgroup = new model_lhbase_authgroup();
			
			$adminUser = $appadminuser->selectOne(['id'=>$adminUserId],'platformId');
			$attributionGroup = $authgroup->select(['attribution'=>$adminUserId],'id')->items;
			$groupIdArr = empty($attributionGroup) ? [0] :  array_column($attributionGroup,'id');
			$userId = self::subordinateId($groupIdArr);
			$authResourc = $authgroupunits->select('adminUserId='.$adminUserId,'unitsId')->items;
			
			$authgroupunits->delete('adminUserId='.$adminUserId);
			if(!empty($resourc)){
				
				if(!empty($authResourc)){
					$nodeIdMiss = array_diff( array_column($authResourc,'unitsId'),$resourc);
					if(!empty($nodeIdMiss)){ //去掉下级所有用户对应的部门
							$authgroupunits->delete('adminUserId in('.implode(',',$userId).') and unitsId in('.implode(',',$nodeIdMiss).')');
					}
				
				}
				$insertAll=[];
				foreach($resourc as $v){
						$insertAll[] = [
							"adminUserId" => $adminUserId,
							"unitsId" => $v,
							];

				}
				
				if(!empty($insertAll)){
							$authgroupunits->beginTransaction(); //批量写入
							$authuseraInser = $authgroupunits->insertall($insertAll);
							if($authuseraInser){
								$authgroupunits->commit();
							} else {
								$authgroupunits->rollBack();
							}
						}
				
				
			}
			/*****资源组存在 end*****/
			
			
            //写入新的权限分配

            if (isset($nodeId) && !empty($nodeId) && is_array($nodeId)) {

                $insertAll = [];
                foreach ($nodeId as $v) {
                    $insertAll[] = [
				'userId'=>$adminUserId,
				'ruleId' =>$v,
				'groupId' =>$groupId,
				'platformId'=>$adminUser['platformId'],
				'appId'=>$appId,
                    ];
                }
				if(!empty($insertAll)){
					$adminuseraccess->beginTransaction(); //批量写入
					$authuseraInser = $adminuseraccess->insertall($insertAll);
					if($authuseraInser){
						$adminuseraccess->commit();
					} else {
						$adminuseraccess->rollBack();
					}
				}
				
					$result->message = '成功';
					$result->code = 1;
				
            } else {

				$result->message = '成功';
				$result->code = 1;
		}
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	
	//获取用户权限组
	public function rightsGroups(AuthorityRequestDTO $authority)
    {
        $result = new ResultDO();
		$platformId = $authority->filename['platformId'];
		$userId = $authority->filename['userId'];
		try {
			$adminuser = new model_lhbase_adminroleuser();
			$role = $adminuser->selectOne(['userId'=>$userId],'roleId');
			/************/
			$group_access = new model_lhbase_adminaccess();
			$groups = $group_access->select(array('roleId' => $role['roleId']),'nodeId')->items;
			$accessId = [];
			foreach($groups as $vc){
				$accessId[$vc['nodeId']] = $vc['nodeId'];
			}
			$dbAccess = new model_lhbase_appadminuseraccess();
			$adminuseraccess = $dbAccess->select(array('userId' => $userId),'nodeId')->items;
			foreach($adminuseraccess as $vg){
				$accessId[$vg['nodeId']] = $vg['nodeId'];
			}
			if(empty($accessId)){
				$data = [[]];
			} else {
				$adminnode = new model_lhbase_adminnode();
				$data = $adminnode->select('id in('.implode(',',$accessId).') and status=1 and isDelete=0', 'id,name')->items;
			}
				$result->data = $data;
				$result->message ='成功';
				$result->code = 1;
        
			/***********/
			//$result->message = json_encode($authority->filename);
			//$result->message = json_encode(array('platform_id' => $platform_id,'userid'=>$userid));
            $result->success = true;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	
	/**
	 * 重设键，转为array(key=>array())
	 * @param array $arr
	 * @param string $key
	 * @return array
	 */
	public function reset_by_key_admin($arr, $key) {
		$re = [];
		foreach ($arr as $v) {
			$re[$v[$key]] = $v;
		}

		return $re;
	}


	/**
	 * 从二维数组中取出自己要的KEY值
	 * @param  array $arrData
	 * @param string $key
	 * @param $im true 返回逗号分隔
	 * @return array
	 */
	public function filter_value_admin($arrData, $key, $im = false) {
		$re = [];
		foreach ($arrData as $k => $v) {
			if (isset($v[$key]))
				$re[] = $v[$key];
		}
		if (!empty($re)) {
			$re = array_flip(array_flip($re));
			sort($re);
		}

		return $im ? implode(',', $re) : $re;
	}
	
	//lower_level_button 查询下级按钮
	public function lower_level_button($parentId,$authRulePid,$parentIds = [0]) {
				if(empty($parentId)) return [0];
				foreach($authRulePid as $v){
					$parentIds[$v] = $parentId[$v];
					//return $parentId;
					if(!empty($parentId[$v]) && $parentId[$v]){

					$parentId = $this->lower_level_button($parentId,[$parentId[$v]],$parentIds);	
					
					}
				}
				return $parentIds;
	}
	
	
	/**
	 * 获取用户的独立授权
	 * @param  array $arrData
	 * @param string $key
	 * @return array
	 */
	public function filter_user_node($arrData, $key='node_id') {
		$re = [];
		$user_access = new model_lhbase_appadminuseraccess();
		$user_node = $user_access->select(array('userid' =>$arrData['userid']),$key)->items;
		foreach($user_node as $v){
			$re[] = $v[$key];
		}
		return $re;

	}
	
	//新建权限组的时候获取
	public function brandNew(AuthorityRequestDTO $authority)
    {

        $result = new ResultDO();
		//$uid = $authority->id;
		$unitId = $authority->filename['unitId'] ? gdl_lib_BaseUtils::getStr($authority->filename['unitId'],'int') : 0;
		$userId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';


		$pid = $authority->filename['pid'] ? gdl_lib_BaseUtils::getStr($authority->filename['pid'],'int') : 0;
		
		$result->code = 0;
        $result->success = true;
		if(!$unitid || !$userid || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			$agkunits = new model_lhbase_units();
			$adminuser = new model_lhbase_appadminuser();
			$adminrole = new model_lhbase_adminrole();
			$types = $agkunits->enabled($userid,$adminReg,$unitid);
			if($types['code']!='y'){
				$result->message = $types['msg'];
				return $result;
			}
			
			
				  //分组信息
			$admingroup = new model_lhbase_adminmodule();
			$list_group = $admingroup->getList();
			$group = self::reset_by_key_admin($list_group, "id");
			//节点信息
			$where_node['status'] = 1;
			$where_node['isDelete'] = 0;
			//对于非超级管理员用户只显示其拥有所有权限的节点
			$AdminAccess = new model_lhbase_adminaccess();
			$adminroleuser = new model_lhbase_adminroleuser();
			$adminuseraccess = new model_lhbase_appadminuseraccess();
			$condition = 'status=1 and isDelete=0';
		
			if($types['mark']['characters']!='zg'){
				//获取管理员的权限组
				$nodeIds = $adminuseraccess->select(['userId'=>$userId,'unitId'=>$unitId],'nodeId')->items;
				if (!empty($nodeIds)){
					$nodeIds = array_column($nodeIds,'nodeId');
					$condition .= " and id in('".implode("','",$nodeIds)."')";
				}
			}
			if($pid){
				$condition.= " and (pid={$pid} or id={$pid})";
			} else {
				$condition.= ' and level < 4';
			}
			$adminnode = new model_lhbase_adminnode();
			$node = $adminnode->select($condition, 'id,pid,group_id,name,title,level,type')->items;



			//生成数据格式
			$node_tree = [];
				foreach ($node as $v) {
					$node_tree[] = [
						"id"          => $v['id'],
						"pid"         => $v['pid'],
						"title"        => $v['title'],
						"value"       => $v['id'] . "_" . $v['level'] . "_" . $v['pid'],
						"showcheck"   => true,
						'operating'=>1,
						'expand'  =>  0,
						'hasChildren' => $v['type'] ? true : false,
						'isexpand'    => true,
						'complete'    => true,
					];
				}
			
	
			$result->data = $node_tree;

            if ($result) {
				$result->datas[][] = $roleId;
				$result->message = '成功';
                $result->code = 1;
            } else {
				$result->message = '失败';
             
            }
      
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	//新建权限组
	public function groupsAdd(AuthorityRequestDTO $authority)
    {

        $result = new ResultDO();
	
		$adminUserId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$title = $authority->filename['title'] ? gdl_lib_BaseUtils::getStr($authority->filename['title'],'string') : '';

		$status = $authority->filename['status'] ? gdl_lib_BaseUtils::getStr($authority->filename['status'],'int') : 0;
		$id = $authority->filename['groupId'] ? gdl_lib_BaseUtils::getStr($authority->filename['groupId'],'int') : 0;
		$appId = $authority->filename['appId'] ? gdl_lib_BaseUtils::getStr($authority->filename['appId'],'int') : 0;
		$result->code = 0;
		$result->success = true;
		if(!$adminUserId || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}

		try {
			/*****************/
			$adminrole = new model_lhbase_authgroup();

			$agkunits = new model_lhbase_units();
			$adminuser = new model_lhbase_appadminuser();
			$datime = time();
			$adminUserDel = $adminuser->selectOne(['id'=>$adminUserId],'platformId');
			$verification = $adminrole->selectOne(['attribution'=>$adminUserId,'title'=>$title,'platformId'=>$adminUserDel['platformId']],'id');
			
			
				
				
			if($id){
				$titleAtion = $adminrole->selectOne(['id'=>$id],'title');
			
				
				if(!empty($verification) && $title!=$titleAtion['title']){
					$result->message='权限组已存在';
					return $result;
				}
				$data = [
					'title' 			=> $title, //'名称',
					'status' 		=> $status, 
					];
				$adminrole->update(['id'=>$id],$data);

			} else {
				
				if(!empty($verification)){
					$result->message='权限组已存在';
					return $result;
				}
				$data = [
					'title' 			=> $title, //'名称',
					'platformId'	=> $adminUserDel['platformId'], //'平台ID',
					'status'		=> 1,
					'appId'			=> $appId,
					'unitsId' 		=> 1, //'单位iid',
					'attribution' 	=> $adminUserId,
					'status' 		=> $status, 
					];
				$id = $adminrole->insert($data);

			}
            if ($id) {
				$result->datas[]['id'] =$id;
				$result->message = '成功';
                $result->code = 1;
            } else {
				$result->message = '失败';
             
            }
      
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	//新建权限组
	public function groupsLook(AuthorityRequestDTO $authority)
    {

		$result = new ResultDO();
		$adminUserId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$groupId = $authority->filename['groupId'] ? gdl_lib_BaseUtils::getStr($authority->filename['groupId'],'int') : 0;
		
		$result->code = 0;
		$result->success = true;
		if(!$adminUserId || !$groupId){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			$adminrole = new model_lhbase_authgroup();
			$agkunits = new model_lhbase_units();
			$res = $adminrole->select(['id'=>$groupId],'id,title,unitsId,status')->items;

			$types = $agkunits->enabled($adminUserId,$adminReg,$res[0]['unitsId']);
			if($types['code']!='y'){
				$result->message = $types['msg'];
				return $result;
			}

            if (!empty($res)) {
			$result->data =$res;
			$result->message = '成功';
			$result->code = 1;
            } else {
				$result->message = '失败';
             
            }
      
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	/*
	public function navigationMenu(AuthorityRequestDTO $authority)
    {

        $result = new ResultDO();

		$unitid = $authority->filename['unitsid'] ? gdl_lib_BaseUtils::getStr($authority->filename['unitsid'],'int') : 0;
		$userid = $authority->filename['adminuserid'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminuserid'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$appid = $authority->filename['appid'] ? gdl_lib_BaseUtils::getStr($authority->filename['appid'],'int') : 0;
		$result->code = 0;
		$result->success = true;
		if(!$unitid || !$userid || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
		
			$agkunits = new model_lhbase_units();
			$adminuser = new model_lhbase_appadminuser();
			$adminnode = new model_lhbase_adminnode();
			$adminuseraccess = new model_lhbase_appadminuseraccess();
			$admingroup = new model_lhbase_adminmodule();
			$AdminAccess =  new model_lhbase_adminaccess();
			$types = $agkunits->enabled($userid,$adminReg,$unitid);
			if($types['code']!='y'){

				$result->message = $types['msg'];
				return $result;
			}
			
			$condition = 'isDelete=0 and status=1';
			if ($types['mark']['characters']!='zg') {
			
				$leftJoin = array(
					$adminuseraccess->tableName().' as role_user' => 'role_user.nodeId = '.$adminnode->tableName().'.id'
					);
					$access_node = $adminnode->select('role_user.userId='.$userid.' and role_user.unitId='.$unitid.' and '.$adminnode->tableName().'.moduleId>0 and '.$adminnode->tableName().".appId={$appid}", $adminnode->tableName().'.moduleId,'. $adminnode->tableName().'.accessCode,'. $adminnode->tableName().'.title','', '', $leftJoin)->items;

			} else {
				$access_node = $adminnode->select("appId={$appid} and isDelete=0",'moduleId,accessCode,title')->items;
			}


            if ($access_node) {
		$result->data = $access_node;
		$result->message = '成功';
              $result->code = 1;
            } else {
		$result->message = '没有数据';
            }
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}*/
	//获取管理员可操作部门
	public function lookeUnits(AuthorityRequestDTO $authority)
    {

		$result = new ResultDO();

		$adminUserId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$groupId = $authority->filename['groupId'] ? gdl_lib_BaseUtils::getStr($authority->filename['groupId'],'int') : 0;
		$title = $authority->filename['title'] ? gdl_lib_BaseUtils::getStr($authority->filename['title'],'string') : '';
		
		$result->code = 0;
		$result->success = true;
		if(!$adminUserId || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}

		try {
			$authgroupunits = new model_lhbase_authgroupunits();
			$appadminuser = new model_lhbase_appadminuser();
			$units = new model_lhbase_units();
			$authuserunits = new model_lhbase_authuserunits();
			$adminDel = $appadminuser->selectOne(['id'=>$adminUserId],'characters,platformId');
		
			$groupUnits = $authgroupunits->select(['groupId'=>$groupId ],'units')->items;
			$groupUnitsId = empty($groupUnits) ? [0] : array_column($groupUnits,'units');
			$condition = 'platformId='.$adminDel['platformId'].' and status=1';
			if($adminDel['characters']!='zg'){
				$unitsId = $authuserunits->select(['adminUserId'=>$adminUserId],'unitsId')->items;
				$unitId = [0];
				if(!empty($unitsId)){
					$unitId = $units->operating($adminDel['platformId'], array_column($unitsId,'unitsId'));
				}
				$condition .= ' and id in('.implode(',',$unitId).')';
			}
			$accessNode = $units->select($condition,'id,unitName')->items;

			$res = [];
			foreach($accessNode as $v){
				$res[] = [
							'id'=>$v['id'],
							'title'=>$v['unitName'],
							'checked'=>in_array($v['id'],$groupUnitsId) ? 1 : 0,
						];
			}
            if (!empty($res)) {
		$result->data = $res;
		$result->message = '成功';
              $result->code = 1;
            } else {
		$result->message = '没有数据';
            }
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	

	public function lookeUserUnits(AuthorityRequestDTO $authority)
    {

		$result = new ResultDO();

		$adminUserId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$userId = $authority->filename['userId'] ? gdl_lib_BaseUtils::getStr($authority->filename['userId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$groupId = $authority->filename['groupId'] ? gdl_lib_BaseUtils::getStr($authority->filename['groupId'],'int') : 0;
		$title = $authority->filename['title'] ? gdl_lib_BaseUtils::getStr($authority->filename['title'],'string') : '';
		
		$result->code = 0;
		$result->success = true;
		if(!$adminUserId || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}

		try {
			$agkunits = new model_lhbase_units();
			$adminuser = new model_lhbase_adminuser();
			$platform = new model_lhbase_platform();
			$authuserunits = new model_lhbase_authuserunits();
			$admin = $adminuser->selectOne(['id'=>$adminUserId,'adminReg'=>$adminReg],'id,characters,platformId,unitsId');
			if($admin['characters']=='zg' && !$admin['unitsId']){
				$unitsId = $agkunits->select('status=1 and isDelete=1','id unitsId')->items;
			} else {
				$unitsId = $authuserunits->select(['adminUserId'=>$adminUserId],'unitsId')->items;
			}
			if(empty($admin['id'])){
				$result->message = '账号错误';
				return $result;
			}
			$unitid = empty($unitsId) ? [0] : array_column($unitsId,'unitsId'); 
			$unitid = array_merge($unitid,[$admin['unitsId']]);
			$node = $agkunits->getMenuSpecial($admin['characters'],$admin['platformId'],$unitid,$title);
			/*************/
			$userUnitsId = $authuserunits->select(['adminUserId'=>$userId],'unitsId')->items;
			$userUnits  = empty($userUnitsId) ? [0] : array_column($userUnitsId,'unitsId');
		

			 if (!empty($node)) {
				$result->data = $node;
				$result->datas[] = $userUnits;
				$result->datas[] = $unitid;
				$result->code = 1;
			} else {
					$result->message = '没有数据';
			}
				return $result;
			
			
			
			
			
			
			
			
			
			
			/*******************
			$authgroupunits = new model_lhbase_authgroupunits();
			$appadminuser = new model_lhbase_appadminuser();
			$units = new model_lhbase_units();
			$authuserunits = new model_lhbase_authuserunits();
			$adminDel = $appadminuser->selectOne(['id'=>$adminUserId],'characters,platformId');
		
			$groupUnits = $authgroupunits->select(['groupId'=>$groupId ],'units')->items;
			$userUnitsId = $authuserunits->select(['adminUserId'=>$userId],'unitsId')->items;
			$userUnits  = empty($userUnitsId) ? [0] : array_column($userUnitsId,'unitsId');
			$groupUnitsId = empty($groupUnits) ? [0] : array_column($groupUnits,'units');
			$groupUnitsId = array_merge($userUnits,$groupUnitsId);
			$condition = 'platformId='.$adminDel['platformId'].' and status=1';
			if($adminDel['characters']!='zg'){
				$unitsId = $authuserunits->select(['adminUserId'=>$adminUserId],'unitsId')->items;
				$unitId = [0];
				if(!empty($unitsId)){
					$unitId = $units->operating($adminDel['platformId'], array_column($unitsId,'unitsId'));
				}
				$condition .= ' and id in('.implode(',',$unitId).')';
			}
			$accessNode = $units->select($condition,'id,parentId,unitName')->items;
			$accessNodeId = [0];
			if(!empty($accessNode)){
				$accessNodeId = array_column($accessNode,'id');
				$getId = $units->getSuperior($adminDel['platformId'],$accessNodeId);
				$getId = array_diff($getId,$accessNodeId);
				if(!empty($getId)){
					$getAccessNode = $units->select('id in('.implode(',',$getId).')','id,parentId,unitName')->items;
					$getAccessNode = empty($getAccessNode) ? [0] : $getAccessNode;
					$accessNode = array_merge($accessNode,$getAccessNode);
				}
			}
			$res = [];
			foreach($accessNode as $v){
				$res[] = [
							'id'=>$v['id'],
							'pid'=>$v['parentId'],
							'title'=>$v['unitName'],
							'checked'=>in_array($v['id'],$groupUnitsId) ? 1 : 0,
							'operating'=>in_array($v['id'],$accessNodeId) ? 1 : 2,
						];
			}
            if (!empty($res)) {
		$result->data = $res;

		$result->message = '成功';
              $result->code = 1;
            } else {
		$result->message = '没有数据';
            }
            return $result;
			**********/
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	
	//获得管理员可操作列表
	public function navigationMenu(AuthorityRequestDTO $authority)
    {

        $result = new ResultDO();

		$unitId = $authority->filename['unitId'] ? gdl_lib_BaseUtils::getStr($authority->filename['unitId'],'int') : 0;
		$userId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$appId = $authority->filename['appId'] ? gdl_lib_BaseUtils::getStr($authority->filename['appId'],'int') : 2;
		$result->code = 0;
		$result->success = true;
		if(!$userId || $adminReg==''){
			$result->message=json_encode($authority->filename);
			return $result;
		}
		
		try {
			/*****************/
			$agkunits = new model_lhbase_units();
			$authuseraccess = new model_lhbase_authuseraccess();
			$authrule = new model_lhbase_authrule();
			$appmenu = new model_lhbase_appmenu();
			$types = $agkunits->enabled($userId,$adminReg);
			if($types['code']!='y'){
				$result->message = $types['msg'];
				return $result;
			}
			//($admin['characters']=='zg' && $admin['unitsId'])
				$identity=($types['mark']['characters']=='zg' && !$types['mark']['unitsId']) ? 'zg' : 'gl';

				$navList = $authrule->getMenu($userId,$appId,$identity);

				$menuId = array_column($navList,'id');
				$rule = $authrule->allMenu();
			//	$buttons = $authrule->buttons($navList,$menuId);
				$menuList = $appmenu->getList($menuId,'ruleId,menuName,icon,id,pid',$appId,$navList);	

				$menu = [];
				foreach($menuList as $v){
					$menu[$v['id']]=	 [
											'id'=>$v['id'],
											'pid'=>$v['pid'],
											'title'=>$v['menuName'],
											'name'=>$rule[$v['ruleId']],
											];
						if($v["pid"]<1){
							$menu[$v['id']]["icon"] = $v["icon"];
						}
				}


            if ($menu) {
		$result->data = $menu;
		//$result->datas = $buttons;
		$result->message ='成功';
              $result->code = 1;
            } else {
		$result->message = '没有数据';
            }
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	public function navigationList(AuthorityRequestDTO $authority)
    {

        $result = new ResultDO();

		$unitId = $authority->filename['unitId'] ? gdl_lib_BaseUtils::getStr($authority->filename['unitId'],'int') : 0;
		$userId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$appId = $authority->filename['appId'] ? gdl_lib_BaseUtils::getStr($authority->filename['appId'],'int') : 2;
		$result->code = 0;
		$result->success = true;
		if(!$userId || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			$agkunits = new model_lhbase_units();
			$authuseraccess = new model_lhbase_authuseraccess();
			$authrule = new model_lhbase_authrule();
			$appmenu = new model_lhbase_appmenu();
			$authuserunits = new model_lhbase_authuserunits();
			$types = $agkunits->enabled($userId,$adminReg);
			if($types['code']!='y'){
				$result->message = $types['msg'];
				return $result;
			}

			if ($types['mark']['characters']!='zg') {
				$navList = $authrule->getMenu($userId,$appId);
				$menuId = $authuseraccess->buttons($userId,$appId);
				$buttons = $authrule->buttons($navList,$menuId);

			} else {
				$condition = "status=1";
				
				if($types['mark']['characters']=='zg' && $types['mark']['unitsId']){
				
					$navList = $authrule->getMenu($userId,$appId);
					$menuId = $authuseraccess->buttons($userId,$appId);
					$buttons = $authrule->buttons($navList,$menuId);
				} else {
					$navList = $authrule->getMenu($userId,$appId,'zg');
					$menuId = $authuseraccess->buttons($userId,$appId);
					$authuseracc = $authrule->select('status=1','id')->items;
					$menuId = empty($authuseracc) ? [0] : array_column($authuseracc,'id');
					$buttons = $authrule->buttons($navList,$menuId);
				}
			}

				$menu = [];
				foreach($navList as $v){
					
						$menu[$v['id']]=	 [
											'title'=>$v['title'],
											'name'=>$v['accessCode'],
											'path'=>$v['name'],
											'component'=>$v['component'],
											];
					
						
				}

				foreach($buttons as $v){
					$menu[$v['attribution']]['permission'][] = $v['name'];
				}

            if ($menu) {
		$result->data[][] = json_encode($menu);

		$result->message = '成功';
              $result->code = 1;
            } else {
		 $result->code = 1;
		$result->message = '没有数据';
            }
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	
	//删除指定管理员
	public function adminDelete(AuthorityRequestDTO $authority)
    {

		$result = new ResultDO();

		$adminUserId = $authority->filename['adminUserId'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminUserId'],'int') : 0;
		$adminReg = $authority->filename['adminReg'] ? gdl_lib_BaseUtils::getStr($authority->filename['adminReg'],'string') : '';
		$userId = $authority->filename['userId'] ? gdl_lib_BaseUtils::getStr($authority->filename['userId'],'int') : 0; //被操作管理员id

	
		$result->code = 0;
		$result->success = true;
		if(!$adminUserId || !$userId || $adminReg==''){
			$result->message='缺少参数';
			return $result;
		}
		
		try {
			/*****************/
			$agkunits = new model_lhbase_units();
			$adminuser = new model_lhbase_appadminuser();
			$adminuseraccess = new model_lhbase_authuseraccess();
			$adminroleuser = new model_lhbase_authuserunits();
			$authgroupaccess = new model_lhbase_authgroupaccess();
			$adminDel = $adminuser->selectOne('id='.$userId,'unitsId');

			$unitsId = $adminuser->operating($adminUserId);
	
			if(!in_array($adminDel['unitsId'],$unitsId) || $userId==$adminUserId){
				$result->message = '无删除管理员权限';
				return $result;
			}
		$adminuser->update(['id'=>$userId],'isDelete=2');
		$adminuseraccess->delete(['userId'=>$userId]);
		$adminroleuser->delete(['adminUserId'=>$userId]);
		$authgroupaccess->delete(['uId'=>$userId]);
		$result->message = '成功';
              $result->code = 1;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	
	//获取需要被删除权限的用户id
	public function deleteRuleUser($userid,$userSetId=[0]) {
		if(empty($userid)) return $userSetId;
		
		$authgroup = new model_lhbase_authgroup();
		$authgroupaccess = new model_lhbase_authgroupaccess();
		//$userId = array_column($uId,'uId');
		/******第一步先查出来每个管理员有多少权限组**********/
		$attributionId = $authgroup->select('attribution in('.implode(',',$userid).')','id')->items;
		if(empty($attributionId)){
			 return $userSetId;
		}
		$userSetId = array_merge($userSetId,$userid);
		$attribution = empty($attributionId) ? [0] : array_column($attributionId,'id');
		
		/******第二步权限组绑定那些管理员**********/
		$uId = $authgroupaccess->select('groupId in('.implode(',',$attribution).')','uId')->items;
		if(!empty($uId)){
			$userId =array_diff(array_column($uId,'uId'),$userSetId); //防止手动写了一条造成死循环的 数据关联
			$userSetId = self::deleteRuleUser($userId,$userSetId);
		}

		return $userSetId;

	}
	
	//循环找下级
	public function subordinateId($groupId) {
			if(empty($groupId)) return [0];
			$authgroupaccess = new model_lhbase_authgroupaccess();
			if(is_array($groupId)){
				$uId = $authgroupaccess->select('groupId in('.implode(',',$groupId).')','uId')->items;
			} else {
				$uId = $authgroupaccess->select(['groupId'=>$groupId],'uId')->items;
			}
			$userId = [0];
			if(!empty($uId)){
				$userId = array_column($uId,'uId');
				$userId = self::deleteRuleUser($userId);
			}
		return $userId;
		
	}
	
}
